package com.example.myapplication.vo;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import com.example.myapplication.until.JBoxUtils;
import com.example.myapplication.until.Globals;
import org.jbox2d.callbacks.ContactImpulse;
import org.jbox2d.dynamics.Body;

public abstract class Item {

    protected Body body;

    private float life = 30;
    private int hittime = 10 ;

    // 记录销毁时的坐标,以及弧度
    private float[] destoryPoint;
    private float destoryAngle;

    public abstract void drawItem(Canvas canvas, Paint paint, Point nowPosition);

    public abstract void hit(Object o, float li , ContactImpulse arg);

    public Body getBody() {
        return body;
    }

    public void setBody(Body body) {
        this.body = body;
        this.body.m_userData = this;
    }

    public void setLife(float life) {
        this.life = life;
        this.hittime-- ;
        if (life <= 0||this.hittime<0) {
            if (destoryPoint == null) {
                destoryPoint = new float[2];
                destoryPoint[0] = body.getPosition().x * Globals.RATE;
                destoryPoint[1] = body.getPosition().y * Globals.RATE;
                destoryAngle = body.getAngle();
            }

            // 当生命小于0时,需要将body销毁
            body.m_userData=null ;
            JBoxUtils.destory(body);
            body=null ;
            //System.out.println("hhaha");
        }
    }

    public float getLife() {
        return life;
    }

    public boolean isDead(){
        return hittime<0||life<0 ;
    }
}
